/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import Beans.ClaseMail;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ReservarHabitacion extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private Connection con;
    public void init() throws ServletException{
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://localhost/Agencia","root","root");
        }
        catch(Exception ex){
            throw new UnavailableException("Error: "+ex.getMessage());
        }
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        if(request.getParameter("numdoc")!=null && request.getParameter("email")!=null){
            try{
                int IdHotel=Integer.parseInt(request.getParameter("id"));
                int NumHab=Integer.parseInt(request.getParameter("num"));
                int NumDoc=Integer.parseInt(request.getParameter("numdoc"));
                String Email=request.getParameter("email");
                SimpleDateFormat formato= new SimpleDateFormat("dd/MM/yyyy");
                HttpSession session=request.getSession();
                String fechaE=session.getAttribute("FechaEntrada").toString();
                Date fecha1= formato.parse(fechaE);
                String fechaS=session.getAttribute("FechaSalida").toString();
                Date fecha2=formato.parse(fechaS);
                int CantHuespedes=((Integer)session.getAttribute("CantHuespedes")).intValue();
                Random rnd=new Random();
                int codReserva=rnd.nextInt();
                CallableStatement cs=con.prepareCall("{call AltaReserva(?,?,?,?,?,?,?)}");
                cs.setInt(1, codReserva);
                cs.setInt(2, NumHab);
                cs.setInt(3, IdHotel);
                Calendar cal= Calendar.getInstance();
                cal.setTime(fecha1);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                cs.setDate(4, new java.sql.Date(cal.getTime().getTime()));
                cal.setTime(fecha2);
                cs.setDate(5, new java.sql.Date(cal.getTime().getTime()));
                cs.setInt(6, CantHuespedes);
                cs.setInt(7, NumDoc);
                cs.executeUpdate();
                ClaseMail mail=new ClaseMail();
                mail.setTo(Email);
                mail.setMessage("Su código de reserva es: "+codReserva);
                mail.Enviar();
                response.sendRedirect("MenuPrincipal.jsp");
            }
            catch(NumberFormatException ex){
                throw new UnavailableException("Error: "+ex.getMessage());
            }
            catch(ParseException ex){
                throw new UnavailableException("Error: "+ex.getMessage());
            }
            catch(SQLException ex){
                throw new UnavailableException("Error: "+ex.getMessage());
            }
            catch(MessagingException ex){
                out.print(ex.getMessage());
            }
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
